using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApi001.Domain;
using WebApi001.Domain.Entity;
using WebApi001.Implement.Data;
using WebApi001.inetrface.Data;

namespace WebApi001.Tools.DbInitialization
{
  public class DbInitialization
  {
    public static void Seed(IServiceProvider service)
    {
      using(var scope = service.CreateScope())
      {
        var db = scope.ServiceProvider.GetService(typeof(WebApi001DoContexe)) as WebApi001DoContexe;
        db.Database.EnsureCreated();

        var hasUsers = db.Users.Any();
        if (!hasUsers)
        {
          var userRes = scope.ServiceProvider.GetService(typeof(IRespository<Users>)) as Efrespository<Users>;
          var roleRes = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as Efrespository<Roles>;
          var userRoleRes = scope.ServiceProvider.GetService(typeof(IRespository<UserRoles>)) as Efrespository<UserRoles>;

          var role = new Roles
          {
            RolesName = "管理员",
            Description = "是一个管理员"
          };
          roleRes.Add(role);
          //db.Roles.Add(role);
          //db.SaveChanges();
          var user = new Users
          {
            Username = "admain",
            Password = "123",

          };
          userRes.Add(user);
          userRoleRes.Add(new UserRoles
          {
            UserId = user.Id,
            RolesId = role.Id
          });
        }
      }
    }
  }
}
